package com.beichuan_travel.controller;

import com.beichuan_travel.model.dto.PlanDTO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.model.vo.PlanVO;
import com.beichuan_travel.service.PlanServcie;
import com.beichuan_travel.utils.ResponseResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "行程接口", tags = "行程接口")
@RequestMapping("plan")
public class PlanController {

    @Resource
    PlanServcie planServcie;

    @Resource
    ResponseResult responseResult;

    @ApiOperation(value = "根据日期查询当天的行程计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "Int"),
            @ApiImplicitParam(name = "date", value = "日期", required = true, dataType = "string", example = "2021-07-21")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/search")
    public ResultVO queryTripByTime(Integer uid, String date) {
        List<PlanVO> voList = planServcie.queryTripPerDay(uid, date);
        return voList != null ? responseResult.ok(voList) : responseResult.empty();
    }

    @ApiOperation(value = "添加行程", notes = "添加行程--sora")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "query", dataType = "Int")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 1002, message = "添加失败")
    })
    @PostMapping("/v1/add")
    public ResultVO addTrip(@RequestBody @Valid PlanDTO planDTO,
                            MultipartFile img,
                            @RequestParam("uid") Integer uid) {
        int type = planDTO.getType();
        if (type < 0) {
            return responseResult.unvalid("type不能小于0");
        } else if (!planServcie.checkMaxType(type)) {
            return responseResult.unvalid("未知type类型");
        }
        int i = planServcie.addTrip(planDTO, img, uid);
        return i > 0 ? responseResult.ok() : responseResult.empty();
    }

    @ApiOperation(value = "删除行程", notes = "删除行程--sora")
    @ApiImplicitParam(name = "tripId", value = "行程安排id", required = true, dataType = "Int", paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1002, message = "删除失败")
    })
    @DeleteMapping("/v1/{tripId}")
    public ResultVO delTrip(@PathVariable("tripId") Integer tripId) {
        int i = planServcie.delTrip(tripId);
        return i > 0 ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation(value = "展示用户所有行程信息")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "path")
    @GetMapping("/v1/{uid}")
    public ResultVO list(@PathVariable("uid") int uid) {
        return responseResult.ok(planServcie.list(uid));
    }


    @ApiOperation(value = "获得行程类型的映射表")
    @GetMapping("/v1/type")
    public ResultVO typeList() {
        return responseResult.ok(planServcie.typeList());
    }


    @ApiOperation(value = "形成详情页的发现，固定每次返回三条记录")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "path")
    @GetMapping("/v1/plan/find")
    public ResultVO find(@PathVariable("uid") int uid) {
        return responseResult.ok();
    }
}
